package com.xzy.simple;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.core.ResolvableType;

/**
 * {@link UserEvent} 事件监听器
 *
 * @author xzy.xiao
 * @date 2022/10/8  19:09
 */
public class UserEventListener implements ApplicationListener<UserEvent> {
    Logger logger = LoggerFactory.getLogger(UserEventListener.class);

    /**
     * Handle an application event.
     *
     * @param event the event to respond to
     */
    @Override
    public void onApplicationEvent(UserEvent event) {
        logger.info("监听到事件：{}", event);
        // TODO:
    }

    public static void main(String[] args) {
        UserEventListener userEventListener = new UserEventListener();
        ResolvableType resolvableType = resolveDeclaredEventType(userEventListener);
        System.out.println(resolvableType);
    }

    /**
     * 解析出指定监听器所关注的事件
     *
     * @param listener 事件监听器
     * @return 监听器关注的事件
     */
    static ResolvableType resolveDeclaredEventType(ApplicationListener<?> listener) {
        return ResolvableType
                .forClass(listener.getClass())
                .as(ApplicationListener.class)
                .getGeneric();
    }
}
